package com.jp.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * Title: TrackCounter
 * Description:
 *
 * @author zifan_jp
 * @version 1.0
 * @since Date: 2019-11-15 17:04
 * Copyright: Copyright (c) zifan_jp
 * ModificationHistory   Who    When    What
 * -----------------------------------------------
 */

@Component
@Aspect
public class TrackCounter {
    private Map<Integer,Integer> trackCounts = new HashMap<>();

    @Pointcut("execution(* com.jp.aop.CompactDisc.playTrack(int,int)) && args(trackNumber,a)")
    public void trackPlayed(int trackNumber,int a){}

    @Before("trackPlayed(trackNumber,a)")
    public void countTrack(int trackNumber,int a){
        int count = getPlayCount(trackNumber);
        trackCounts.put(trackNumber,count+1);
    }

    public int   getPlayCount(int trackNumber){
        return trackCounts.containsKey(trackNumber)? trackCounts.get(trackNumber):0;
    }
}
